/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package studentinforedux;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author theInterloper
 */
public class ReadWrite {
    
    private static final String CATALOG_XML_OUT = "./student_catalog2.xml";

    public static StudentDB read() throws JAXBException, IOException {
      
        JAXBContext context = JAXBContext.newInstance(StudentDB.class);
        Unmarshaller um = context.createUnmarshaller();  
        StudentDB studentdb = (StudentDB) um.unmarshal((new FileReader(CATALOG_XML_OUT))); 
        return studentdb;
        
    }
    
    static void write (StudentDB studentdb)throws JAXBException, IOException {
   
        JAXBContext context = JAXBContext.newInstance(StudentDB.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(studentdb, System.out);
        
        Writer w = null;
        try{
            w = new FileWriter(CATALOG_XML_OUT);
            m.marshal(studentdb, w);
            }
        finally{
            try{
                w.close();
            } catch (Exception e){
                
            }
        }
    }
}
